package com.syf.chatting.Service;
import com.syf.chatting.Dao.UserRepository;
import com.syf.chatting.domain.User;
import com.syf.chatting.utils.MyUtil;
import com.syf.chatting.vo.Result;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.transaction.Transactional;

/**
 * Created by sunyf on 2017/10/10.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@Service
@Transactional
public class UserServiceImpl  {

    @Autowired
    private UserRepository userDao;


    public Result registerNewUser(User user) {
        User oldUser = userDao.findByPhoneOrNameAndAppId(user.getPhone(),user.getName(),user.getAppId());
        if(oldUser != null){
            if(oldUser.getPhone() == user.getPhone()){
                return new Result(1000,"该手机号已被注册",null);
            }else if(oldUser.getName().equals(user.getName())){
                return new Result(1000,"该用户名已被注册",null);
            }
            return MyUtil.errResult;
        }else{
            User user1 = userDao.saveAndFlush(user);
            return new Result(2000,"",user1);
        }
    }
}
